
coos.ajax = function()
{
	this.version = "1.0";
};
coos.ajax.createRequest = function()
{
	var objXMLHttp = null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
		if (objXMLHttp.overrideMimeType)
		{
			objXMLHttp.overrideMimeType('text/xml');
		}
	}
	else
	{
	    var MSXML = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0'];
	    for(var n = 0; n < MSXML.length; n++)
	    {
	        try
	        {
	            objXMLHttp = new ActiveXObject(MSXML[n]);
	            break;
	        }
	        catch(e) {}
	    }
	}
  	return objXMLHttp;
};
coos.ajax.get = function(url,callbackFunction,extend,asynchronous)
{
	var http_request = coos.ajax.createRequest();
	coos.ajax.bindResponse (http_request,callbackFunction,extend);
	url = coos.url.random(url);
	asynchronous = asynchronous || true;
	http_request.open ("GET", url, asynchronous);
	http_request.send (null);
	return false;
};
coos.ajax.post = function(url,callbackFunction,formElement,extend,encode,asynchronous)
{
	var http_request = coos.ajax.createRequest();
	coos.ajax.bindResponse(http_request,callbackFunction,extend);
	url = coos.url.random(url);
	asynchronous = asynchronous || true;
	formElement = coos.$obj(formElement);
	var requireString = coos.ajax.parseParameteres(formElement,encode);
	http_request.open("POST", url, asynchronous);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	http_request.setRequestHeader("encoding","utf-8");
	http_request.setRequestHeader("Content-length", requireString.length);
	if(requireString == null){requireString = "";}
	http_request.send(requireString);
	return false;
};
coos.ajax.postText = function(url,callbackFunction,formElement,extend,asynchronous)
{
	coos.ajax.post(url,callbackFunction,formElement,extend,"text",asynchronous);
};
coos.ajax.postUnencode = function(url,callbackFunction,formElement,extend,asynchronous)
{
	coos.ajax.post(url,callbackFunction,formElement,extend,"unencode",asynchronous);
};
coos.ajax.jsonp = function(url,callbackFunction,responseName,extend)
{
	var script = document.createElement("script");
	script.id = "coos_jsonp_" + responseName;
    if(script.addEventListener)
    {
		script.addEventListener("load", function(){
			coos.ajax.jsonp.callbackHandle(callbackFunction,responseName,extend);
			}, false);
    }
	else if(script.attachEvent)
	{
		script.attachEvent("onreadystatechange",function(){
			var target = window.event.srcElement;
			if(target.readyState == "loaded")
			{
				coos.ajax.jsonp.callbackHandle(callbackFunction,responseName,extend);
			}
			});
	}
	if(responseName)
	{
		url = coos.url.addAttribe("responseName",responseName,url);
	}
	url = coos.url.random(url);
	script.src = url;
	coos.$head().appendChild(script);
};
coos.ajax.jsonp.callbackHandle = function(callbackFunction,responseName,extend)
{
 	try
	{
		var responseText = eval(responseName);
		if(typeof(responseText) == "object")
		{
			responseText = coos.json.toJSONString(responseText);
		}
		callbackFunction["responseText"] = responseText;
		var args = [];
		args.push(responseText);
		if(typeof(extend) == "object" && extend !== null)
		{
			for (var property in extend)
			{
				callbackFunction[property] = extend[property];
				args.push(extend[property]);
			}
		}
		callbackFunction.apply(this,args);
		coos.$head().removeChild(coos.$id("coos_jsonp_" + responseName));
	}
	catch(e)
	{
	}
};
coos.ajax.encode = function(str,encode)
{
	if(typeof(encode) == "undefined"){encode = "encode";}
	if (str == null || str == "" || encode=="unencode")
	{
		return str;
	}
	var value = "";
	if(encode == "text")
	{
		value = str.replace(/\n|\r|\r\n/g,"");
		value = value.replace(/<\/?.+?>/g,"");
		value = encodeURIComponent(encodeURIComponent(value)).replace(/%20/g, "+");
	}
	else
	{
		value = encodeURIComponent(encodeURIComponent(str)).replace(/%20/g, "+");
	}
	return value;
};
coos.ajax.parseParameteres = function(formElement,encode)
{
	formElement = coos.$obj(formElement);
	var rs = "";
	rs += coos.ajax.getValue(formElement.getElementsByTagName("input"),"input",encode);
	rs += coos.ajax.getValue(formElement.getElementsByTagName("select"),"select",encode);
	rs += coos.ajax.getValue(formElement.getElementsByTagName("textarea"),"textarea",encode);
	return rs;
};
coos.ajax.getValue = function(elements,type,encode)
{
	var len = elements.length;
	if(len == 0)return "";
	var s = "";
	if(type == "input")
	{
		for (var i = 0; i < len; i++)
		{
			var obj = elements[i];
			if (obj.type == "hidden")
			{
				s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
			}
			else if (obj.type == "text")
			{
				s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
			}
			else if (obj.type == "password")
			{
				s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
			}
			else if (obj.type == "checkbox")
			{
				if (obj.checked)
				{
					s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
				}
			}
			else if (obj.type == "radio")
			{
				if (obj.checked)
				{
					s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
				}
			}
		}
	}
	else if(type == "textarea")
	{
		for (var i = 0; i < len; i++)
		{
			var obj = elements[i];
			s += obj.name + "=" + coos.ajax.encode(obj.value,encode) + "&";
		}
	}
	else if(type == "select")
	{
		for (var i = 0; i < len; i++)
		{
			var obj = elements[i];
			if (obj.type == "select")
			{
				if (obj.selectedIndex > -1)
					s += obj.name + "=" + coos.ajax.encode(obj.options[obj.selectedIndex].value,encode) + "&";
			}
			else if (obj.type == "select-one")
			{
				if (obj.selectedIndex > -1)
					s += obj.name + "=" + coos.ajax.encode(obj.options[obj.selectedIndex].value,encode) + "&";
			}
		}
	}
	return s;
};
coos.ajax.bindResponse = function(http_request,callbackFunction,extend)
{
	if(typeof(callbackFunction) != "function")
	{
		alert("请传入回调函数!");
		return false;
	}
	var responseText = "";
	var isTiemout = false;
	setTimeout(function(){isTimeout = true;},5000);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4 && !isTiemout)
		{
			if (http_request.status == 200)
			{
				responseText = http_request.responseText;
				try
				{
					callbackFunction["responseText"] = responseText;
					var args = [];
					args.push(responseText);
					if(typeof(extend) == "object" && extend !== null)
					{
						for (var property in extend)
						{
							callbackFunction[property] = extend[property];
							args.push(extend[property]);
						}
					}
					callbackFunction.apply(this,args);
					try{http_request = null;}catch(e){}
				}
				catch(e)
				{
					alert("实际错误消息为：" + e.message + "\n错误类型字符串为：" + e.name);
				}
			}
			else
			{
				var status = http_request.status;
				switch (status)
				{
					case 403:
						alert("你请求的页面禁止访问!");
						break;
					case 404:
						alert("你请求的页面不存在!");
						break;
					case 500:
						alert("你请求的页面服务器内部错误!");
						break;
					case 502:
						alert("Web服务器收到无效的响应!");
						break;
					case 503:
						alert("服务器繁忙，请稍后再试!");
						break;
					default:
						alert("你请求的页面遇到问题，操作失败!错误代码:" + status);
						break;
				}
			}
		}
	};
};
